package com.fxpc.util;

import com.fxpc.common.vo.ProvinceCityAreaCode;
import com.fxpc.hddc.activefault.dto.primary.entity.DivisionEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class ProvinceCityAreaUtil {
    private static final List<ProvinceCityAreaCode> pcaList = new ArrayList<>();

    /**
     * 将省市区归类放在对应的一个对象中
     *
     * @param divisionlist 数据库查询到的所有区划数据
     */
    public static void initProvinceCityArea(List<DivisionEntity> divisionlist) {
        //查询数据库
        for (DivisionEntity division : divisionlist) {
            String divisionName = division.getDivisionName();
            ProvinceCityAreaCode provinceCityAreaCode = new ProvinceCityAreaCode();
            provinceCityAreaCode.setCode(division.getDivisionCode());
            String[] split = division.getAllParentId().split(",");
            if (split.length == 2) {
                //省
                provinceCityAreaCode.setProvince(division.getDivisionName());
                provinceCityAreaCode.setProvinceCode(division.getDivisionCode());
            } else if (split.length == 3) {
                Map<String, String> pMap = getCityName(split[1], divisionlist);
                String pcode = pMap.keySet().iterator().next();
                provinceCityAreaCode.setProvince(pMap.get(pcode));
                provinceCityAreaCode.setProvinceCode(pcode);
                provinceCityAreaCode.setCity(pMap.get(pcode));
                provinceCityAreaCode.setCityCode(pcode);
                if ("北京市".equals(pMap.get(pcode)) || "重庆市".equals(pMap.get(pcode)) || "上海市".equals(pMap.get(pcode)) || "天津市".equals(pMap.get(pcode))) {
                    provinceCityAreaCode.setArea(division.getDivisionName());
                    provinceCityAreaCode.setAreaCode(division.getDivisionCode());
                }
            } else if (split.length == 4) {
                //县
                Map<String, String> pMap = getCityName(split[1], divisionlist);
                String pcode = pMap.keySet().iterator().next();
                provinceCityAreaCode.setProvince(pMap.get(pcode));
                provinceCityAreaCode.setProvinceCode(pcode);
                Map<String, String> cMap = getCityName(split[2], divisionlist);
                String ccode = cMap.keySet().iterator().next();
                provinceCityAreaCode.setCity(cMap.get(ccode));
                provinceCityAreaCode.setCityCode(ccode);
                provinceCityAreaCode.setArea(division.getDivisionName());
                provinceCityAreaCode.setAreaCode(division.getDivisionCode());
            }
            pcaList.add(provinceCityAreaCode);
        }

    }

    /**
     * 根据 division_id 获取 省市区名称
     *
     * @param id   省市区的唯一标识
     * @param list 所有的省市区数据集合
     * @return
     */
    public static Map<String, String> getCityName(String id, List<DivisionEntity> list) {
        Map<String, String> map = new HashMap<>();
        for (DivisionEntity divisionEntity : list) {
            if (divisionEntity.getDivisionId().equals(id)) {
                map.put(divisionEntity.getDivisionCode(), divisionEntity.getDivisionName());
                return map;
            }
        }
        return null;
    }

    /**
     * 根据 省市区名称获取区划编码
     *
     * @param provinceName
     * @param cityName
     * @param areaName
     * @return code
     */
    public static String getCode(String provinceName, String cityName, String areaName) {
        String code = null;
        for (ProvinceCityAreaCode pca : pcaList) {
            if (StringUtils.isEmpty(cityName)) {
                if (provinceName.equals(pca.getProvince())) {
                    code = pca.getProvinceCode();
                    break;
                }
            }
            if (StringUtils.isNotEmpty(cityName) && StringUtils.isEmpty(areaName)) {
                if (provinceName.equals(pca.getProvince()) && cityName.equals(pca.getCity())) {
                    code = pca.getCityCode();
                    break;
                }
            }

            if (StringUtils.isNotEmpty(areaName)) {
                if (provinceName.equals(pca.getProvince()) && cityName.equals(pca.getCity()) && areaName.equals(pca.getArea())) {
                    code = pca.getAreaCode();
                    break;
                }
            }
        }
        return code;
    }

}
